<?php
namespace app\office\controller;

use think\Request;
use think\Db;
use app\office\controller\OfficeBase;
use app\office\model\Employee as EmployeeModel;
use app\office\model\Organization as OrganizationModel;

class EmployeeOrganization extends OfficeBase
{
    protected $employee_filter=[];
    protected $organization_filter=[];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员组织机构列表");
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['employee_is_sth','employee_sth_num','employee_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("employee_","",$k);
            $this->employee_filter[$k]=$v;
          }
        }
      }
      // $employee_filter_msg=$this->employee_filter;
      $employee=EmployeeModel::scope('static')->where($this->employee_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=EmployeeModel::all(); */
      foreach($employee as $k=>$v){
        $employee_list[$v->id]=$v;
        $employee_list[$v->id]['organization']=$v->organization;
      }
      $countall=EmployeeModel::scope('static')->where($this->employee_filter)->count();
      $this->assign('employee_list',$employee_list);
      $this->assign('countall',$countall);
      return $this->fetch();
    }
    
    /**
     * 显示资源列表(组织机构为主体)
     *
     * @return \think\Response
     */
    public function index_organization()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"组织机构-雇员列表");        
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['organization_is_sth','organization_sth_num','organization_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("organization_","",$k);
            $this->organization_filter[$k]=$v;
          }
        }
      }
      // $employee_filter_msg=$this->employee_filter;
      $organization=OrganizationModel::scope('static')->where($this->organization_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=EmployeeModel::all(); */
      foreach($organization as $k=>$v){
        $organization_list[$v->id]=$v;
        $organization_list[$v->id]['organization']=$v->employee;
      }
      $countall=OrganizationModel::scope('static')->where($this->organization_filter)->count();
      $this->assign('organization_list',$organization_list);
      $this->assign('countall',$countall);
      return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员组织机构分配(批量)"); 
      // 此处根据是否有搜索信息来进行筛选
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['employee_is_sth','employee_sth_num','employee_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("employee_","",$k);
            $this->employee_filter[$k]=$v;
          }
          if($v!=null& in_array($k,['organization_is_sth','organization_sth_num','organization_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("organization_","",$k);
            $this->organization_filter[$k]=$v;
          }
        }
      }
      // 此处是获取employee清单
      //注:此处使用了scope('static')后,不会显示static=0,即'删除'的数据
      $list=EmployeeModel::scope('static')->where($this->employee_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=EmployeeModel::all(); */
      $countall=EmployeeModel::scope('static')->where($this->employee_filter)->count();
      $this->assign('employee_list',$list);
      $this->assign('employee_countall',$countall);
      // 此处是获取organization清单
      $list=OrganizationModel::scope('static')->where($this->organization_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=OrganizationModel::all(); */
      $countall=OrganizationModel::scope('static')->where($this->organization_filter)->count();
      $this->assign('organization_list',$list);
      $this->assign('organization_countall',$countall);
      return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $msg=array();
      $data=input('request.');
      if(empty($data['select_employee'])){
        $this->error("请选择雇员",'create','',1);
      }
      $select_employee_id=$data['select_employee'];
      if(empty($data['select_organization'])){
        $this->error("请选择组织机构",'create','',1);
      }
      $select_organization_id=$data['select_organization'];
      // dump($select_organization);exit;
      foreach($select_employee_id as $emp_id){
        $employee=EmployeeModel::getById($emp_id);
        // 取出$employee已经写入数据库的org_id;
        $org_id_in_database=Db::name('employee_organization')->field('organization_id')->where('employee_id',$emp_id)->select();
        // 给查询出的数据降维
        foreach($org_id_in_database as $k=>$v){
          $org_id_in_database[$k]=$v['organization_id'];
        }
        $select_organization_id=array_diff($select_organization_id,$org_id_in_database);
        if(empty($select_organization_id)){
          $text="雇员:".$employee->title."所选组织机构均已分配过";
          array_push($msg,$text);
        }
        foreach($select_organization_id as $org_id){
          $organization=OrganizationModel::getById($org_id);
          $employee->organization()->attach($organization);
          $text="雇员:".$employee->title."组织机构:".$organization->title."分配完毕";
          array_push($msg,$text);
        }
      }
      dump($msg);
      $this->success("操作完成",'index','',1);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员组织机构信息");     
      $employee=EmployeeModel::get($id,'organization');
      $employee_organization=$employee->organization;
      // dump( $employee_organization);
      $this->assign('employee',$employee);
      $this->assign('employee_organization',$employee_organization);
      return $this->fetch();
      // return view();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员组织机构编辑");
      $employee=EmployeeModel::getById($id);
      $employee_organization=$employee->organization;
      $this->assign('employee',$employee);
      $this->assign('employee_organization',$employee_organization);
      $countall=OrganizationModel::scope('static')->count();
      $this->assign('countall',$countall);
      $organization_tree=OrganizationModel::getTree();
      $this->assign('organization_tree',$organization_tree);
      // dump($organization_tree);
      return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        dump(input('post.'));
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($emp_id,$org_id)
    {
      $employee=EmployeeModel::getById($emp_id);
      $organization=OrganizationModel::getById($org_id);
      $employee->organization()->detach($organization);
      $this->redirect('read',['id'=>$emp_id]);
    }
}
